{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import *\n",
    "from collections import Counter\n",
    "import seaborn as sns\n",
    "from tqdm import tqdm\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'retina'\n",
    "IMAGE_DIR = 'image_contest_level_2'\n",
    "CROP_DIR = 'crop'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def disp2(img):\n",
    "    cv2.imwrite('a.png', img)\n",
    "    return Image('a.png')\n",
    "\n",
    "def disp(img, txt=None, first=False):\n",
    "    global index\n",
    "    if first:\n",
    "        index = 1\n",
    "        plt.figure(figsize=(16, 9))\n",
    "    else:\n",
    "        index += 1\n",
    "    plt.subplot(4, 3, index)\n",
    "    if len(img.shape) == 2:\n",
    "        plt.imshow(img, cmap='gray')\n",
    "    else:\n",
    "        plt.imshow(img[:,:,::-1])\n",
    "    if txt:\n",
    "        plt.title(txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot(index):\n",
    "    global img, gray, b, eq, bw, m1, m2, r, roi\n",
    "    img = cv2.imread('%s/%d.png'%(IMAGE_DIR, index))\n",
    "    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    eq = cv2.equalizeHist(gray)\n",
    "    b = cv2.medianBlur(eq, 9)\n",
    "    \n",
    "    m, n = img.shape[:2]\n",
    "    b = cv2.resize(b, (n//4, m//4))\n",
    "\n",
    "    m1 = cv2.morphologyEx(b, cv2.MORPH_OPEN, np.ones((7, 40)))\n",
    "    m2 = cv2.morphologyEx(m1, cv2.MORPH_CLOSE, np.ones((3, 3)))\n",
    "    _, bw = cv2.threshold(m2, 127, 255, cv2.THRESH_BINARY_INV)\n",
    "    \n",
    "    bw = cv2.resize(bw, (n, m))\n",
    "\n",
    "    r = img.copy()\n",
    "    img2, ctrs, hier = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
    "    for ctr in ctrs:\n",
    "        x, y, w, h = cv2.boundingRect(ctr)\n",
    "        cv2.rectangle(r, (x, y), (x+w, y+h), (0, 255, 0), 10)\n",
    "    x, y, w, h = cv2.boundingRect(np.vstack(ctrs))\n",
    "    \n",
    "    d = 10\n",
    "    cv2.rectangle(r, (x-d, y-d), (x+w+d, y+h+d), (0, 0, 255), 10)\n",
    "    roi = img[max(0, y-d):min(m, y+h+d),max(0, x-d):min(n, x+w+d)]\n",
    "\n",
    "    disp(img, 'raw img', 1)\n",
    "    disp(eq, 'eq')\n",
    "    disp(b, 'blur')\n",
    "    disp(m2, 'm')\n",
    "    disp(r, 'rect')\n",
    "    disp(roi, 'roi')\n",
    "    \n",
    "    for i, ctr in enumerate(ctrs):\n",
    "        x, y, w, h = cv2.boundingRect(ctr)\n",
    "        roi = img[max(0, y-d):min(m, y+h+d),max(0, x-d):min(n, x+w+d)]\n",
    "        disp(roi, 's%d'%i)\n",
    "    \n",
    "# plot(53698)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('size_test.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(df['r'] > 3).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.plotting.scatter_matrix(df, alpha=0.1, figsize=(14,8), diagonal='kde');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('size.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Counter(df['m'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Counter(df['n'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(df['r'] == 2).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(df['r'] == 3).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(df['r'] > 3).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.plotting.scatter_matrix(df, alpha=0.1, figsize=(14,8), diagonal='kde');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(99982)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 公式有两个和三个的差别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot.scatter('h', 'w')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 出现的位置 (x, y) 与框的宽度和高度 (w, h) 没有关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot.scatter('h', 'x')\n",
    "df.plot.scatter('h', 'y')\n",
    "\n",
    "df.plot.scatter('w', 'x')\n",
    "df.plot.scatter('w', 'y')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 公式数量与高度正相关"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot.scatter('h', 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
